﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Diagnostics;
using KC.DataAccess.Meta;
using DAL = KC.DataAccess.Meta.Meta;
using Logger = KC.Logging.Logger;

namespace KC.Business.Meta
{
	public static partial class Meta
	{

		public static IQueryable<JobRun> GetOverdueJobs(ref MetaDataContext dc)
		{
			if (dc == null) throw new ArgumentNullException("dc");
			DAL.EnqueueOverdueJobs(ref dc);
			var OverdueJobs = DAL.GetOverdueJobs(ref dc); 
			return OverdueJobs;
		}
		public static IQueryable<JobRun> GetPendingJobs(ref MetaDataContext dc)
		{
			if (dc == null) throw new ArgumentNullException("dc");
			var PendingJobs = DAL.GetPendingJobs(ref dc);
			foreach (JobRun jr in PendingJobs) {
				Debug.WriteLine("Enqueuing job run " + jr.Id.ToString());
				jr.JobServer = System.Net.Dns.GetHostName();
				jr.Started = DateTime.Now;
				jr.Status = JobStatus.Idle.ToString();
				jr.StatusUpdated = DateTime.Now; }
			try { dc.SubmitChanges(); }
			catch (ChangeConflictException) { }
			catch (Exception ex) { Logger.LogError(Logger.Project.Meta, Logger.ErrorLevel.Error, ex.ToString()); }
			return PendingJobs;
		}

		public static void ProcessJobMV(ref MetaDataContext dc, ref JobRun jr) {
			switch (jr.Job.Name) {
				default:
					throw new ArgumentNullException("Name");
			}

		}

	}

}
